package com.itheima.protocol.masterSelector;

import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.SerializableSerializer;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * master选取测试
 */
public class MasterChooseTest {


    private final static String CONNECTIONSTRING = "192.168.1.108:2181";
    private final static int SESSIONTIMEOUT = 5000;

    public static void main(String[] args) {

        //存放所有的zkClient列表
        List<MasterSelector> selectorLists = new ArrayList<>();
        try {
            for (int i = 0; i < 10; i++) {
                ZkClient client = new ZkClient(CONNECTIONSTRING, SESSIONTIMEOUT, 5000
                        , new SerializableSerializer());

                UserCenter userCenter = new UserCenter();
                userCenter.setMc_id(i);
                userCenter.setMc_name("客户端: " + i);

                MasterSelector selector = new MasterSelector(userCenter,client);
                selectorLists.add(selector);

                selector.start();
                TimeUnit.SECONDS.sleep(4);

            }


        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            //循环去停止
            for (MasterSelector selector:selectorLists){
                selector.stop();
            }
        }
    }
}
